<!-- 房屋管理 -->
<template>
  <div>
    <div class="header">
      <div class="title" style="height: 65px">
        <div style="margin-bottom: 5px; margin-top: 5px; margin-left: 10px">
          <a-space style="margin-left: 10px">
            <span>开始时间：</span>
            <a-date-picker v-model:value="startDate" :format="dateFormat" />
          </a-space>
          <a-space style="margin-left: 10px">
            <span>结束时间：</span>
            <a-date-picker v-model:value="endDate" :format="dateFormat" />
          </a-space>
          <a-space style="margin-left: 10px; margin-top: 15px">
            <span>关键字：</span>
            <a-input
              v-model:value="txtKeyWord"
              placeholder="请输入姓名、地址或房屋编号进行查询"
              style="width: 300px; margin-top: -10px"
            />
          </a-space>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a-space class="search-btn">
            <a-button type="primary" @click="searchBtn">查询</a-button>
          </a-space>
          <space style="float: right; margin-right: 10px; margin-top: 15px">
            <a-button type="primary"> 导出房屋台账</a-button>
          </space>
        </div>
      </div>
      <div style="width: 100%; background: #fff; height: 40px">
        <div style="width: 45%; float: left; font-size: 16px; padding-left: 20px; line-height: 37px"
          ><home-outlined two-tone-color="#eb2f96" /><span style="padding: 0px 10px">
            {{ title }}</span
          ></div
        >
        <div
          style="
            width: 30%;
            float: right;
            font-size: 16px;
            padding-left: 20px;
            line-height: 37px;
            text-align: right;
            padding-right: 20px;
          "
          ><a style="color: blue" @click="getBack"
            ><arrow-left-outlined two-tone-color="#eb2f96" style="padding: 0px 10px" />返回上一页</a
          ></div
        >
      </div>
      <a-table
        style="margin-top: 0px"
        :dataSource="tableData"
        :columns="displayColumns"
        :pagination="false"
        :scroll="{ x: 1500, y: tableHeight }"
        bordered
        :loading="loading"
      />
      <div class="pagination" style="float: right">
        <div style="float: left; line-height: 32px; padding-right: 12px">共{{ total }}条记录</div>
        <a-pagination
          show-size-changer
          show-total
          v-model:current="pageIndex"
          v-model:pageSize="pageSize"
          :total="total"
          style="float: right"
          @show-size-change="onShowSizeChange"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import moment from 'moment';
  import dayjs from 'dayjs';
  import { ref, reactive, toRefs, onMounted, onBeforeMount, computed, watch } from 'vue';
  import { HomeOutlined, ArrowLeftOutlined } from '@ant-design/icons-vue';
  import { useRouter, useRoute } from 'vue-router';
  import { HouseList } from '/@/api/demo/api'; //获取房屋列表
  import { getRegion, delethouseApi } from '/@/api/demo/system'; //统计工作进展
  import { useUserStore } from '/@/store/modules/user';
  import { iTableData, iTableColumns } from './types/table';
  import { iParams } from './types/params';
  import { useCheckStore } from '/@/store/modules/checkStore';
  let userInfo = ref<any>(null);
  let options = ref<any[]>([]);
  const title = ref<string>(''); // 开始时间
  let detailTableHeight = ref<number>(0); // 表格高度
  let tableHeight = ref<number>(0); // 表格高度
  let pageIndex = ref(1);
  let pageSize = ref(15);
  const dateFormat = 'YYYY-MM-DD';
  const startDate = ref<string>(''); // 开始时间
  const endDate = ref<any>(null); // 结束时间
  const value18 = ref<string[]>([]);
  let txtKeyWord = ref<string>('');
  let zczt = ref<string>('');
  let jczt = ref<string>('');
  let zc_yhzt = ref<string>('');
  let jc_yhzt = ref<string>('');
  let fwzt = ref<string>('正常出租');
  let total = ref(0);
  const route = useRoute();
  const { xzq, xzqdm, town, towndm, villagedm, village, startTime, endTime, currentTitle } =
    route.query;
  // 使用 useRouter 创建一个 router 实例
  const router = useRouter();
  let data = reactive<{
    tableData: iTableData[];
    tableColumns: iTableColumns[];
    wscolsList: { wch: number }[];
    id: number | any;
    loading: Boolean;
    keyWorld: String;
    currentqx: string; // 当前行政区名称
    currentTitle: string;
    cityToken: string | any;
  }>({
    // 表格数据
    tableData: [],
    tableColumns: [
      {
        title: '序号',
        dataIndex: 'index',
        key: 'index',
        align: 'center',
        width: 80,
      },

      {
        title: '区名称',
        dataIndex: 'xzq',
        key: 'xzq',
        align: 'center',
        width: 80,
      },
      {
        title: '街/乡镇名称',
        dataIndex: 'town',
        key: 'town',
        align: 'center',
        width: 120,
      },
      {
        title: '村/社区名称',
        dataIndex: 'village',
        key: 'village',
        align: 'center',
        width: 150,
      },
      {
        title: '用户姓名',
        dataIndex: 'hzxm',
        key: 'hzxm',
        align: 'center',
        width: 100,
      },
      {
        title: '地址',
        dataIndex: 'address',
        key: 'address',
        align: 'center',
        width: 280,
      },
      {
        title: '房屋编号',
        dataIndex: 'fwbh',
        key: 'fwbh',
        align: 'left',
        width: 180,
        customCell: (row) => {
          let html = row.remark;
          if (html != '' && html != null) {
            html = `<span style="display: inline-flex; align-items: center; gap: 4px">
                      <svg viewBox="64 64 896 896" style="width: 1em; height: 1em; margin-right: 4px">
                        <!-- Outer circle with pulsating animation -->
                        <path 
                          d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z" 
                          fill="#FF0000"
                          style="
                            animation: pulse 1.5s infinite;
                            opacity: 0.8;
                          "
                        ></path>
                        
                        <!-- Inner circle (solid red) -->
                        <circle cx="512" cy="512" r="300" fill="#FF0000"></circle>
                        
                        <!-- Exclamation mark (white) -->
                        <path 
                          d="M464 336a48 48 0 1 0 96 0 48 48 0 1 0-96 0zm72 112h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V456c0-4.4-3.6-8-8-8z" 
                          fill="#FFFFFF"
                        ></path>
                        
                        <!-- Glare effect (optional) -->
                        <circle cx="350" cy="350" r="80" fill="#FFFFFF" opacity="0.2"></circle>
                        
                        <style>
                          @keyframes pulse {
                            0% { transform: scale(1); opacity: 0.8; }
                            50% { transform: scale(1.05); opacity: 1; }
                            100% { transform: scale(1); opacity: 0.8; }
                          }
                        </style>
                  </svg>
    <span style="color: blue; cursor: pointer; text-decoration: underline">
      ${row.fwbh}
    </span>
  </span>`;
          } else {
            html = `<span style=\"color: blue; cursor: pointer; text-decoration: underline\">${row.fwbh}</span>`;
          }
          return {
            innerHTML: html,
            onClick: async () => {
              getDetailData(row);
            },
          };
        },
      },
      {
        title: '房屋状态',
        dataIndex: 'fwzt',
        key: 'fwzt',
        align: 'center',
        width: 100,
        customCell: (row) => {
          let style = '';
          if (row.fwzt == '正常出租') {
            style = 'color: green;';
          } else if (row.fwzt == '不出租') {
            style = 'color: blue;';
          } else if (row.fwzt == '已拆除') {
            style = 'color: red;';
          }
          return {
            style: style,
            class: 'cell-hover',
          };
        },
      },
      {
        title: '最近一次自查时间',
        dataIndex: 'zc_create_on',
        key: 'zc_create_on',
        align: 'center',
        width: 200,
      },
      {
        title: '最近一次检查时间',
        dataIndex: 'jc_create_on',
        key: 'jc_create_on',
        align: 'center',
        width: 200,
      },
      {
        title: '最近一次乡镇核查时间',
        dataIndex: 'jz_create_on',
        key: 'jc_create_on',
        align: 'center',
        width: 250,
      },
      {
        title: '最近一次区级核查时间',
        dataIndex: 'qj_create_on',
        key: 'qj_create_on',
        align: 'center',
        width: 250,
      },
      {
        title: '最近一次市级核查时间',
        dataIndex: 'sj_create_on',
        key: 'sj_create_on',
        align: 'center',
        width: 250,
      },
    ],
    id: null,
    loading: true,
    keyWorld: '',
    cityToken: '', // 判断是不是市区 110000 是北京市 展示搜索宽
    currentqx: '', // 当前行政区名称
    currentTitle: '', // 当前详情标题
    wscolsList: [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }],
  });
  // 表格的 ref Dom名称
  let { tableData, loading } = toRefs(data);
  const getCode = async () => {
    if (startTime != null && startTime != '') {
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      const sTime = moment(startTime + '').format('YYYY-MM-DD');
      const eTime = moment(endTime + '').format('YYYY-MM-DD');
      const start: any = dayjs(sTime);
      const end: any = dayjs(eTime);
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      startDate.value = start;
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      endDate.value = end;
    }
  };
  async function getData() {
    if (startDate.value == null) {
      let currentDate = moment().format('YYYY-MM-DD');
      const start: any = dayjs(currentDate);
      const end: any = dayjs(currentDate);
      startDate.value = start;
      endDate.value = end;
    }
    let params: iParams = {
      pageIndex: pageIndex.value,
      pageSize: pageSize.value,
      txtKeyWord: txtKeyWord.value,
      fwzt: fwzt.value,
      zczt: zczt.value,
      jczt: jczt.value,
      zc_yhzt: zc_yhzt.value,
      jc_yhzt: jc_yhzt.value,
      startTime: startTime + '',
      endTime: endTime + '',
      xzqdm: xzqdm + '',
      towndm: towndm + '',
      villagedm: villagedm + '',
    };
    console.log('*****', params);
    loading.value = true;
    let res: any = await HouseList(params);
    console.log('@res--工作进入汇总表', res.items);

    if (res || res.items.length > 0) {
      loading.value = false;
      data.tableData = [...res.items];
      console.log('@res--工作进入汇总表', tableData);
      total.value = res.total;
      title.value = currentTitle + '(总栋数：' + res.total + '栋)';
    }
  }

  function getUserInfo() {
    const userStore = useUserStore();
    userInfo.value = userStore?.getUserInfo;
  }

  function searchBtn() {
    data.loading = true;
   // value18.value = [];
    getData();
  }

  async function getRegionData() {
    let res: any = await getRegion();
    options.value = res;
  }
  function onChange(value) {
    value18.value = value;
  }
  const displayColumns = computed(() => {
    const { role } = userInfo.value;
    let arr: any[] = [];
    if (role === 'City') {
      // qx
      arr = data.tableColumns.filter((item) => item.key !== 'jzmc' && item.key !== 'czmc');
      return arr;
    } else if (role === 'Area') {
      // jz
      arr = data.tableColumns.filter((item) => item.key !== 'czmc' && item.key !== 'qxmc');
      return arr;
    } else if (role === 'Street') {
      // cun
      arr = data.tableColumns.filter((item) => item.key !== 'jzmc' && item.key !== 'qxmc');
      console.log('@Aarr', arr);
      return arr;
    } else {
      return data.tableColumns;
    }
  });
  //跳转记录页面
  function getDetailData(row: any) {
    router.push({
      path: '/utils/houseCheckLog',
      query: {
        xzq: row.xzq,
        town: row.town,
        village: row.village,
        hzxm: row.hzxm,
        fwbh: row.fwbh,
        startTime: startTime + '',
        endTime: endTime + '',
      },
    });
  }
  const getBack = () => {
    let params = {
      startTime: startTime,
      endTime: endTime,
      xzqdm: xzqdm + '',
      towndm: towndm + '',
      currentTitle: '【' + xzq + '-' + town + '】房屋基本信息情况',
    };
    const store = useCheckStore();
    store.setData(params); // 存入Pinia

    router.push({
      path: 'villagebaseHourse',
    });
  };
  const onShowSizeChange = () => {
    // antDesign  自动给  pageIndex 和 pageSize 赋值
    //  pageIndex.value = pageIndex;
    //  pageSize.value = pageSize;
    // console.log('@pageIndex,pageSize', pageIndex, pageSize);
  };
  const getTableHeight = () => {
    setTimeout(() => {
      detailTableHeight.value = window.innerHeight;
      tableHeight.value = window.innerHeight;
    });
  };
  watch(pageSize, () => {
    console.log('pageSize', pageSize.value);
    getData();
  });
  watch(pageIndex, () => {
    console.log('current', pageIndex.value);
    getData();
  });
  onBeforeMount(() => {
    getUserInfo();
  });
  onMounted(() => {
    console.log('******工作进展统计初始化******');
    getCode();
    getRegionData();
    getData();
    getTableHeight();
    // getTableData();
  });
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
  #map {
    height: calc(100vh - 140px);
  }
  .col-sql {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .x-houseMng-search {
    z-index: 100;
    margin: 6px;
    position: absolute;
  }

  .root-house-mng-main {
    margin: 6px;
  }

  .x-village-tabs {
    //z-index: 100;
    //position: absolute;
    width: 60vw;
    margin-top: 6px;
    margin-left: 40px;
    //background-color: rgba(34, 34, 34, 0.6);
    :deep(.ant-form) {
      margin-top: 6px;
    }

    // :deep(.ant-form-item-label) > label {
    //   color: white;
    // }
  }
</style>
